package com.apollographql.apollo.subscription;

//import HOS.core.view.PointerIconCompat;
//import HOS.viewbinding.BuildConfig;
//import com.apollographql.apollo.subscription.SubscriptionTransport;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicReference;
import com.kt.Metadata;
import com.kt.jvm.internal.DefaultConstructorMarker;
import com.kt.jvm.internal.Intrinsics;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okio.Buffer;

@Metadata(bv = {1, 0, 3}, d1 = {"\u0000`\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0010\u0003\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0010\u000e\n\u0002\b\u0003\u0018\u00002\u00020\u0001:\u0002'(B)\b\u0007\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\u0006\u0010\u0006\u001a\u00020\u0007\u0012\b\b\u0002\u0010\b\u001a\u00020\t¢\u0006\u0002\u0010\nJ\b\u0010\u0013\u001a\u00020\u0014H\u0016J\u0010\u0010\u0015\u001a\u00020\u00142\u0006\u0010\u0016\u001a\u00020\u0017H\u0016J\r\u0010\u0018\u001a\u00020\u0014H\u0000¢\u0006\u0002\b\u0019J\u0017\u0010\u001a\u001a\u00020\u00142\b\u0010\u001b\u001a\u0004\u0018\u00010\u001cH\u0000¢\u0006\u0002\b\u001dJ\u0017\u0010\u001e\u001a\u00020\u00142\b\u0010\u0016\u001a\u0004\u0018\u00010\u001fH\u0000¢\u0006\u0002\b J\r\u0010!\u001a\u00020\u0014H\u0000¢\u0006\u0002\b\"J\b\u0010#\u001a\u00020\u0014H\u0002J\u0010\u0010$\u001a\u00020\u00142\u0006\u0010\u0016\u001a\u00020\u0017H\u0016J\f\u0010%\u001a\u00020&*\u00020\u0017H\u0002R\u000e\u0010\u0006\u001a\u00020\u0007X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\tX\u0004¢\u0006\u0002\n\u0000R\u001a\u0010\u000b\u001a\b\u0012\u0004\u0012\u00020\r0\fX\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u000e\u0010\u000fR\u000e\u0010\u0004\u001a\u00020\u0005X\u0004¢\u0006\u0002\n\u0000R\u001a\u0010\u0010\u001a\b\u0012\u0004\u0012\u00020\u00110\fX\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0012\u0010\u000fR\u000e\u0010\u0002\u001a\u00020\u0003X\u0004¢\u0006\u0002\n\u0000¨\u0006)"}, d2 = {"Lcom/apollographql/apollo/subscription/WebSocketSubscriptionTransport;", "Lcom/apollographql/apollo/subscription/SubscriptionTransport;", "webSocketRequest", "Lokhttp3/Request;", "webSocketConnectionFactory", "Lokhttp3/WebSocket$Factory;", "callback", "Lcom/apollographql/apollo/subscription/SubscriptionTransport$Callback;", "serializer", "Lcom/apollographql/apollo/subscription/OperationMessageSerializer;", "(Lokhttp3/Request;Lokhttp3/WebSocket$Factory;Lcom/apollographql/apollo/subscription/SubscriptionTransport$Callback;Lcom/apollographql/apollo/subscription/OperationMessageSerializer;)V", "webSocket", "Ljava/util/concurrent/atomic/AtomicReference;", "Lokhttp3/WebSocket;", "getWebSocket$apollo_runtime", "()Ljava/util/concurrent/atomic/AtomicReference;", "webSocketListener", "Lcom/apollographql/apollo/subscription/WebSocketSubscriptionTransport$WebSocketListener;", "getWebSocketListener$apollo_runtime", "connect", "", "disconnect", "message", "Lcom/apollographql/apollo/subscription/OperationClientMessage;", "onClosed", "onClosed$apollo_runtime", "onFailure", "t", "", "onFailure$apollo_runtime", "onMessage", "Lcom/apollographql/apollo/subscription/OperationServerMessage;", "onMessage$apollo_runtime", "onOpen", "onOpen$apollo_runtime", "BuildConfig.BUILD_TYPE", "send", "serializeToJson", "", "Factory", "WebSocketListener", "apollo-runtime"}, k = 1, mv = {1, 4, 0})
/* compiled from: WebSocketSubscriptionTransport.kt */
public final class WebSocketSubscriptionTransport implements SubscriptionTransport {
    private final Callback callback;
    private final OperationMessageSerializer serializer;
    private final AtomicReference<WebSocket> webSocket;
    private final WebSocket.Factory webSocketConnectionFactory;
    private final AtomicReference<WebSocketListener> webSocketListener;
    private final Request webSocketRequest;

    public WebSocketSubscriptionTransport(Request request, WebSocket.Factory factory, Callback callback2) {
        this(request, factory, callback2, null, 8, null);
    }

    public WebSocketSubscriptionTransport(Request webSocketRequest2, WebSocket.Factory webSocketConnectionFactory2, Callback callback2, OperationMessageSerializer serializer2) {
        Intrinsics.checkParameterIsNotNull(webSocketRequest2, "webSocketRequest");
        Intrinsics.checkParameterIsNotNull(webSocketConnectionFactory2, "webSocketConnectionFactory");
        Intrinsics.checkParameterIsNotNull(callback2, "callback");
        Intrinsics.checkParameterIsNotNull(serializer2, "serializer");
        this.webSocketRequest = webSocketRequest2;
        this.webSocketConnectionFactory = webSocketConnectionFactory2;
        this.callback = callback2;
        this.serializer = serializer2;
        this.webSocket = new AtomicReference<>();
        this.webSocketListener = new AtomicReference<>();
    }

    /* JADX INFO: this call moved to the top of the method (can break code semantics) */
    public /* synthetic */ WebSocketSubscriptionTransport(Request request, WebSocket.Factory factory, Callback callback2, OperationMessageSerializer operationMessageSerializer, int i, DefaultConstructorMarker defaultConstructorMarker) {
        this(request, factory, callback2, (i & 8) != 0 ? ApolloOperationMessageSerializer.INSTANCE : operationMessageSerializer);
    }

    public final AtomicReference<WebSocket> getWebSocket$apollo_runtime() {
        return this.webSocket;
    }

    public final AtomicReference<WebSocketListener> getWebSocketListener$apollo_runtime() {
        return this.webSocketListener;
    }

    @Override // com.apollographql.apollo.subscription.SubscriptionTransport
    public void connect() {
        WebSocketListener webSocketListener2 = new WebSocketListener(this);
        if (this.webSocketListener.compareAndSet(null, webSocketListener2)) {
            this.webSocket.set(this.webSocketConnectionFactory.newWebSocket(this.webSocketRequest, webSocketListener2));
            return;
        }
        throw new IllegalStateException("Already connected".toString());
    }

    @Override // com.apollographql.apollo.subscription.SubscriptionTransport
    public void disconnect(OperationClientMessage message) {
        Intrinsics.checkParameterIsNotNull(message, "message");
        WebSocket andSet = this.webSocket.getAndSet(null);
        if (andSet != null) {
            andSet.close(1001, serializeToJson(message));
        }
        release();
    }

    @Override // com.apollographql.apollo.subscription.SubscriptionTransport
    public void send(OperationClientMessage message) {
        Intrinsics.checkParameterIsNotNull(message, "message");
        WebSocket socket = this.webSocket.get();
        if (socket != null) {
            socket.send(serializeToJson(message));
        } else {
            this.callback.onFailure(new IllegalStateException("Send attempted on closed connection"));
        }
    }

    public final void onOpen$apollo_runtime() {
        this.callback.onConnected();
    }

    public final void onMessage$apollo_runtime(OperationServerMessage message) {
        this.callback.onMessage(message);
    }

    public final void onFailure$apollo_runtime(Throwable t) {
        try {
            this.callback.onFailure(t);
        } finally {
            release();
        }
    }

    public final void onClosed$apollo_runtime() {
        try {
            this.callback.onClosed();
        } finally {
            release();
        }
    }

    private final void release() {
        WebSocketListener andSet = this.webSocketListener.getAndSet(null);
        if (andSet != null) {
            andSet.release();
        }
        this.webSocket.set(null);
    }

    private final String serializeToJson(OperationClientMessage $this$serializeToJson) {
        Buffer buffer = new Buffer();
        try {
            this.serializer.writeClientMessage($this$serializeToJson, buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer.readUtf8();
    }

    @Metadata(bv = {1, 0, 3}, d1 = {"\u0000B\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0010\u0003\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0005\b\u0000\u0018\u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003¢\u0006\u0002\u0010\u0004J \u0010\b\u001a\u00020\t2\u0006\u0010\n\u001a\u00020\u000b2\u0006\u0010\f\u001a\u00020\r2\u0006\u0010\u000e\u001a\u00020\u000fH\u0016J \u0010\u0010\u001a\u00020\t2\u0006\u0010\n\u001a\u00020\u000b2\u0006\u0010\f\u001a\u00020\r2\u0006\u0010\u000e\u001a\u00020\u000fH\u0016J\"\u0010\u0011\u001a\u00020\t2\u0006\u0010\n\u001a\u00020\u000b2\u0006\u0010\u0012\u001a\u00020\u00132\b\u0010\u0014\u001a\u0004\u0018\u00010\u0015H\u0016J\u0018\u0010\u0016\u001a\u00020\t2\u0006\u0010\n\u001a\u00020\u000b2\u0006\u0010\u0017\u001a\u00020\u000fH\u0016J\u0018\u0010\u0018\u001a\u00020\t2\u0006\u0010\n\u001a\u00020\u000b2\u0006\u0010\u0014\u001a\u00020\u0015H\u0016J\u0006\u0010\u0019\u001a\u00020\tR\u001c\u0010\u0005\u001a\u0010\u0012\f\u0012\n \u0007*\u0004\u0018\u00010\u00030\u00030\u0006X\u0004¢\u0006\u0002\n\u0000¨\u0006\u001a"}, d2 = {"Lcom/apollographql/apollo/subscription/WebSocketSubscriptionTransport$WebSocketListener;", "Lokhttp3/WebSocketListener;", "delegate", "Lcom/apollographql/apollo/subscription/WebSocketSubscriptionTransport;", "(Lcom/apollographql/apollo/subscription/WebSocketSubscriptionTransport;)V", "delegateRef", "Ljava/lang/ref/WeakReference;", "com.kt.jvm.PlatformType", "onClosed", "", "webSocket", "Lokhttp3/WebSocket;", "code", "", "reason", "", "onClosing", "onFailure", "t", "", "response", "Lokhttp3/Response;", "onMessage", "text", "onOpen", "BuildConfig.BUILD_TYPE", "apollo-runtime"}, k = 1, mv = {1, 4, 0})
    /* compiled from: WebSocketSubscriptionTransport.kt */
    public static final class WebSocketListener extends okhttp3.WebSocketListener {
        private final WeakReference<WebSocketSubscriptionTransport> delegateRef;

        public WebSocketListener(WebSocketSubscriptionTransport delegate) {
            Intrinsics.checkParameterIsNotNull(delegate, "delegate");
            this.delegateRef = new WeakReference<>(delegate);
        }

        @Override // okhttp3.WebSocketListener
        public void onOpen(WebSocket webSocket, Response response) {
            Intrinsics.checkParameterIsNotNull(webSocket, "webSocket");
            Intrinsics.checkParameterIsNotNull(response, "response");
            WebSocketSubscriptionTransport webSocketSubscriptionTransport = this.delegateRef.get();
            if (webSocketSubscriptionTransport != null) {
                webSocketSubscriptionTransport.onOpen$apollo_runtime();
            }
        }

        @Override // okhttp3.WebSocketListener
        public void onMessage(WebSocket webSocket, String text) {
            Intrinsics.checkParameterIsNotNull(webSocket, "webSocket");
            Intrinsics.checkParameterIsNotNull(text, "text");
            WebSocketSubscriptionTransport delegate = this.delegateRef.get();
            if (delegate != null) {
                Intrinsics.checkExpressionValueIsNotNull(delegate, "delegateRef.get() ?: return");
                try {
                    delegate.onMessage$apollo_runtime(delegate.serializer.readServerMessage(new Buffer().writeUtf8(text)));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override // okhttp3.WebSocketListener
        public void onFailure(WebSocket webSocket, Throwable t, Response response) {
            Intrinsics.checkParameterIsNotNull(webSocket, "webSocket");
            Intrinsics.checkParameterIsNotNull(t, "t");
            WebSocketSubscriptionTransport webSocketSubscriptionTransport = this.delegateRef.get();
            if (webSocketSubscriptionTransport != null) {
                webSocketSubscriptionTransport.onFailure$apollo_runtime(t);
            }
        }

        @Override // okhttp3.WebSocketListener
        public void onClosing(WebSocket webSocket, int code, String reason) {
            Intrinsics.checkParameterIsNotNull(webSocket, "webSocket");
            Intrinsics.checkParameterIsNotNull(reason, "reason");
            WebSocketSubscriptionTransport webSocketSubscriptionTransport = this.delegateRef.get();
            if (webSocketSubscriptionTransport != null) {
                webSocketSubscriptionTransport.onClosed$apollo_runtime();
            }
        }

        @Override // okhttp3.WebSocketListener
        public void onClosed(WebSocket webSocket, int code, String reason) {
            Intrinsics.checkParameterIsNotNull(webSocket, "webSocket");
            Intrinsics.checkParameterIsNotNull(reason, "reason");
            WebSocketSubscriptionTransport webSocketSubscriptionTransport = this.delegateRef.get();
            if (webSocketSubscriptionTransport != null) {
                webSocketSubscriptionTransport.onClosed$apollo_runtime();
            }
        }

        public final void release() {
            this.delegateRef.clear();
        }
    }

    @Metadata(bv = {1, 0, 3}, d1 = {"\u00000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000e\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\u0018\u00002\u00020\u0001B!\b\u0007\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\b\b\u0002\u0010\u0006\u001a\u00020\u0007¢\u0006\u0002\u0010\bJ\u0010\u0010\u000b\u001a\u00020\f2\u0006\u0010\r\u001a\u00020\u000eH\u0016R\u000e\u0010\u0006\u001a\u00020\u0007X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0004\u001a\u00020\u0005X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\t\u001a\u00020\nX\u0004¢\u0006\u0002\n\u0000¨\u0006\u000f"}, d2 = {"Lcom/apollographql/apollo/subscription/WebSocketSubscriptionTransport$Factory;", "Lcom/apollographql/apollo/subscription/SubscriptionTransport$Factory;", "webSocketUrl", "", "webSocketConnectionFactory", "Lokhttp3/WebSocket$Factory;", "serializer", "Lcom/apollographql/apollo/subscription/OperationMessageSerializer;", "(Ljava/lang/String;Lokhttp3/WebSocket$Factory;Lcom/apollographql/apollo/subscription/OperationMessageSerializer;)V", "webSocketRequest", "Lokhttp3/Request;", "create", "Lcom/apollographql/apollo/subscription/SubscriptionTransport;", "callback", "Lcom/apollographql/apollo/subscription/SubscriptionTransport$Callback;", "apollo-runtime"}, k = 1, mv = {1, 4, 0})
    /* compiled from: WebSocketSubscriptionTransport.kt */
    public static final class Factory implements SubscriptionTransport.Factory {
        private final OperationMessageSerializer serializer;
        private final WebSocket.Factory webSocketConnectionFactory;
        private final Request webSocketRequest;

        public Factory(String str, WebSocket.Factory factory) {
            this(str, factory, null, 4, null);
        }

        public Factory(String webSocketUrl, WebSocket.Factory webSocketConnectionFactory2, OperationMessageSerializer serializer2) {
            Intrinsics.checkParameterIsNotNull(webSocketUrl, "webSocketUrl");
            Intrinsics.checkParameterIsNotNull(webSocketConnectionFactory2, "webSocketConnectionFactory");
            Intrinsics.checkParameterIsNotNull(serializer2, "serializer");
            this.webSocketConnectionFactory = webSocketConnectionFactory2;
            this.serializer = serializer2;
            Request build = new Request.Builder().url(webSocketUrl).addHeader("Sec-WebSocket-Protocol", "graphql-ws").addHeader("Cookie", "").build();
            Intrinsics.checkExpressionValueIsNotNull(build, "Request.Builder()\n      …ie\", \"\")\n        .build()");
            this.webSocketRequest = build;
        }

        /* JADX INFO: this call moved to the top of the method (can break code semantics) */
        public /* synthetic */ Factory(String str, WebSocket.Factory factory, OperationMessageSerializer operationMessageSerializer, int i, DefaultConstructorMarker defaultConstructorMarker) {
            this(str, factory, (i & 4) != 0 ? ApolloOperationMessageSerializer.INSTANCE : operationMessageSerializer);
        }

        @Override // com.apollographql.apollo.subscription.SubscriptionTransport.Factory
        public SubscriptionTransport create(Callback callback) {
            Intrinsics.checkParameterIsNotNull(callback, "callback");
            return new WebSocketSubscriptionTransport(this.webSocketRequest, this.webSocketConnectionFactory, callback, this.serializer);
        }
    }
}
